/**
 * Created by wangchun on 15/9/16.
 */
define(function (require, exports, module) {

    var global=require("js/global");//加载全局js
    var dialog=require("js/dialog");//加载对话框
    module.exports = function(app){
        //添加页面控制器的ctrl
        app.controller('configTableEditCtrl', ['$scope','$state','requestService','$modal', function($scope,$state,requestService,$modal) {
            $scope.formData={};
            $scope.fields=[];
            if(table.scaffoldXmlFieldList!=null && table.scaffoldXmlFieldList!=''){//获取
                $scope.fields=JSON.parse(table.scaffoldXmlFieldList);
            }
            $scope.formData.scaffoldXmlFieldList=JSON.stringify($scope.fields);
            $scope.addFields=function(){
                dealFields(null,"add");
            }
            $scope.editFields=function(filedId){
                dealFields(filedId);
            }
            function dealFields(filedId,type){
                var editObj={};
                if(type!="add"){
                    for(var i= 0,len=$scope.fields.length;i<len;i++){
                        var nowFiledId=$scope.fields[i].filedId;
                        if(nowFiledId==filedId){
                            editObj=$scope.fields[i];
                            break;
                        }
                    }
                }

                var ModalInstanceCtrl = function ($scope, $modalInstance, content,fields) {
                    $scope.alert={title: "编辑字段对话框",content :content};
                    $scope.editDialog=fields;
                    $scope.editTempDialog={};
                    $scope.editTempDialog.isHidden=true;
                    $scope.ok = function () {
                        $modalInstance.close($scope.editDialog);
                    };

                    $scope.cancel = function () {
                        $modalInstance.close(false);
                    };
                    $scope.setJavaType=function(val){
                        $scope.editDialog.javaType=$scope.editTempDialog.javaSelectType;
                        $scope.editTempDialog.isHidden=true;
                    }
                    $scope.showSelect=function(bool){
                        $scope.editTempDialog.isHidden=bool;
                    }

                };
                var modalInstance = $modal.open({
                    templateUrl:G.path.moduleResPath+'/table/edit.htm',
                    controller: ModalInstanceCtrl,
                    size: 800,
                    resolve: {
                        content:function(){
                            return "1112";
                        },
                        fields:function(){
                            return editObj;
                        }
                    }
                });
                modalInstance.result.then(function(obj){
                    if(!obj){
                        return;
                    }
                    if(type=="add"){
                        $scope.fields.push(editObj);
                    }
                    $scope.formData.scaffoldXmlFieldList=JSON.stringify($scope.fields);
                });
            }
            $scope.delFields=function(filedId){
                for(var i= 0,len=$scope.fields.length;i<len;i++){
                    var nowFiledId=$scope.fields[i].filedId;
                    if(nowFiledId==filedId){
                        $scope.fields.splice(i,1);
                        break;
                    }
                }
                $scope.formData.scaffoldXmlFieldList=JSON.stringify($scope.fields);
            }
            $scope.submitSave=function(module){
                var submitUrl="/config/table/save.html";
                requestService.submit(submitUrl,$scope.formData).success(function(response){
                    if(!global.isSuccess(response)){
                        return;
                    }
                    $state.go("tableList",{"module":module}, {reload: true});//保存后直接跳转到列表页面
                });
            }

        }]);
    }

});
